package org.dream.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Enumeration;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class JTableUtil {
	public static final Font TABLE_FONT= new Font("宋体", Font.PLAIN, 15);
	// 自动设置列宽
	public static void fitTableColumns(JTable table) {
		JTableHeader header = table.getTableHeader();
		int rowCount = table.getRowCount();
		Enumeration columns = table.getColumnModel().getColumns();
		while (columns.hasMoreElements()) {
			TableColumn column = (TableColumn) columns.nextElement();
			int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
			int width = (int) table.getTableHeader().getDefaultRenderer()
					.getTableCellRendererComponent(table, column.getIdentifier(), false, false, -1, col)
					.getPreferredSize().getWidth();
			for (int row = 0; row < rowCount; row++) {
				int preferedWidth = (int) table.getCellRenderer(row, col)
						.getTableCellRendererComponent(table, table.getValueAt(row, col), false, false, row, col)
						.getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth);
			}
			header.setResizingColumn(column); // 此行很重要
			column.setWidth(width + table.getIntercellSpacing().width);
		}
	}

	// 手动设置列宽
	public static void fitTableColumns(JTable table, int[] columnWidths) {
		for (int i = 0; i < columnWidths.length; i++) {
			table.getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
		}
	}

	public static void setUI(JTable table) {
		table.setDefaultRenderer(Object.class, new MyTableCellRenderer());
		table.setBackground(Color.BLACK);
		table.setFillsViewportHeight(true);
		table.setRowHeight(20);
		table.setFont(TABLE_FONT);
		table.setShowGrid(false);
		// table.setGridColor(Color.YELLOW);
		table.setForeground(Color.YELLOW);
		table.getTableHeader().setDefaultRenderer(new MyTableHeaderCellRenderer());
		table.setSelectionBackground(table.getBackground());
		table.setSelectionForeground(table.getForeground());
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				if (row % 2 == 0)
					setBackground(Color.BLACK);
				else if (row % 2 == 1)
					setBackground(new Color(40, 40, 40));
				return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

			}
		};
		for (int i = 0; i < table.getColumnCount(); i++) {
			table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
		}
		JTableUtil.fitTableColumns(table);

	}

	static class MyTableCellRenderer extends DefaultTableCellRenderer {
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
				boolean hasFocus, int row, int column) {
			
			super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			// int zcolumn = ((JTable) table).zcolumn;
			// if (zcolumn != -1) {
			// try {
			// double zhangfu = (Double) table.getValueAt(row, zcolumn);
			// if (zhangfu > 0.0) {
			// setForeground(Color.RED);
			// } else {
			// setForeground(Color.GREEN);
			// }
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
			// }

			return this;
		}
	}

	static class MyTableHeaderCellRenderer extends DefaultTableCellRenderer {
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
				boolean hasFocus, int row, int column) {
			super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			this.setFont(TABLE_FONT);
			this.setForeground(Color.WHITE);
			return this;
		}
	}
}
